package com.example.dziennikposilkow;

import org.xmlpull.v1.XmlPullParser;
import java.util.ArrayList;

import com.example.dziennikposilkow.importXML.XMLReader;
import com.example.dziennikposilkow.model.Products;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TabelaProdukty extends DziennikMainActivity implements
		ScrollViewListener {
	/** Called when the activity is first created. */
	TableLayout country_table;
	TableLayout name_table;
	ObservableScrollView scrollView1;
	ObservableScrollView scrollView2;

	private int subcategoryID;
	public XMLReader xmlReader = new XMLReader();
	public Products[] products;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

/*		Bundle extras = getIntent().getExtras();
		if (extras == null) {
		    return;
		    }
		// Get data via the key
		String value1 = extras.getString(Intent.EXTRA_TEXT);
		if (value1 != null) {
		*/

		String value1 = this.getIntent().getStringExtra("name");
		Log.d("VALUE1", value1);
		for (int i = 0; i < xmlReader.subcategories.length; i++){
			if (xmlReader.subcategories[i].equalsIgnoreCase(value1))	{
		        		subcategoryID = i;
		        		Log.d("subcategory", subcategoryID+"");
		        		break;
		    }
		}
		
		setContentView(R.layout.activity_tabela_produkty);
		country_table = (TableLayout) findViewById(R.id.country_table);
		name_table = (TableLayout) findViewById(R.id.name_table);

		scrollView1 = (ObservableScrollView) findViewById(R.id.ScrollView1);
		scrollView2 = (ObservableScrollView) findViewById(R.id.ScrollView2);
		scrollView1.setScrollViewListener(this);
		scrollView2.setScrollViewListener(this);
		
       // products = xmlReader.readAllData(getResources().getXml(R.xml.produkty));
        //wczytanie produktow
		/*XMLReader xmlReader = new XMLReader();
        products = new Products[xmlReader.subcategories.length];
        for (int i = 0; i < xmlReader.subcategories.length; i++){
        	products[i] = new XMLReader().readData(getResources().getXml(R.xml.produkty), xmlReader.subcategories[i]);
        }
		*/

		products = Singleton.getInstance().products;
		
		fillCountryTable();
		fillNameTable();
	}

	void scrollView() {

	}

	void fillCountryTable() {

		TableRow row;
		
		ArrayList<TextView> textView = new ArrayList<TextView>();
		
		// Converting to dip unit
		int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				(float) 1, getResources().getDisplayMetrics());
		
		
		/*for (int i = 0; i < 34; i++) {
			TextView t = new TextView(this);

			if (i % 2 == 0) {
				t.setTextColor(getResources().getColor(R.color.dark_red));
			} else {
				t.setTextColor(getResources().getColor(R.color.blue));
			}
			t.setTypeface(null, 1);
			//t.setText(CountriesList.abbreviations[current]);
			t.setTextSize(18);
			t.setPadding(0, 0, 0, 15);
			t.setWidth(200 * dip);
			if (i < 2 || i == 4 || i == 7 || i == 23 || i == 32) {
				t.setWidth(200 * dip);
			} else if (i < 7) {
				t.setWidth(300 * dip);
			} else if (i < 24) {
				t.setWidth(150 * dip);
			} else {	
				t.setWidth(110 * dip);
			}   
			
			textView.add(t);

		}
	*/
		
		//int iloscProduktow = textView.size();
		int iloscProduktow = products[subcategoryID].productsArray.length;
		
		for (int current = 0; (current < iloscProduktow || current < 13) ; current++) {
			row = new TableRow(this);
			
				for (int i = 0; i < 34 ;i++){
				
					TextView t = new TextView(this);

				if (current % 2 == 0) {
					//t.setTextColor(getResources().getColor(R.color.dark_red));
					t.setBackgroundColor(getResources().getColor(R.color.green1));
				} else {
					//t.setTextColor(getResources().getColor(R.color.blue));
					t.setBackgroundColor(getResources().getColor(R.color.green2));
				}
				t.setTextColor(getResources().getColor(R.color.black));
				t.setTypeface(null, 1);
				t.setGravity(Gravity.CENTER);
				//t.setText(CountriesList.abbreviations[current]);
				t.setTextSize(18);
				t.setPadding(0, 7, 0, 8);
				t.setWidth(200 * dip);
				if (i < 2) { // || i == 4 || i == 7 || i == 23 || i == 32
					t.setWidth(130 * dip);
				} else if (i == 3) {
					t.setWidth(160 * dip);
				} else if (i == 5 || i == 6 || i == 23) {
					t.setWidth(200 * dip);
				} else if (i == 8 || (i > 23 && i < 32)) {
					t.setWidth(110 * dip);
				} else  {
					t.setWidth(150 * dip);
				}  
				if (current < iloscProduktow){
				t.setText(products[subcategoryID].productsArray[current].getProductInfoField(i + 2)); }
				else
				{
					t.setText(" ");
				}
				row.addView(t);
				}
		
			
			
			
			
			//*********************************************
			/*for (int j = 0; j < textView.size(); j++) {
				textView.get(j).setText(
						products[0].productsArray[current]
								.getProductInfoField(j + 2)); // +2, bo bez id i nazwy
				// textView.get(j).setText(CountriesList.abbreviations[current]);
				if (textView.get(j).getParent() == null) {
					Log.d("aa", "" + j);
					row.addView(textView.get(j));
				}
			}*/
				
			
				//textView.get(i).setText(CountriesList.abbreviations[current]);
				//row.addView(textView.get(i));
			
			country_table.addView(row, new TableLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		}

	}

	void fillNameTable() {
		TableRow row;
		TextView t_name;
		// Converting to dip unit
		int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				(float) 1, getResources().getDisplayMetrics());

		for (int current = 0; current < products[subcategoryID].productsArray.length; current++) {
			row = new TableRow(this);

			t_name = new TextView(this);
			if (current % 2 == 0) {
				t_name.setTextColor(getResources().getColor(R.color.green1));
			} else {
				t_name.setTextColor(getResources().getColor(R.color.green2));
			}

			t_name.setText(products[subcategoryID].productsArray[current].getProductInfoField(1));

			t_name.setTypeface(null, 1);

			t_name.setTextSize(18);

			t_name.setWidth(300 * dip);

			row.addView(t_name);

			t_name.setPadding(0, 7, 0, 8);

			name_table.addView(row, new TableLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			// t_name.setOnTouchListener(new CustomTouchListener());

		}
	}

	public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
			int oldx, int oldy) {

		if (scrollView == scrollView1) {
			scrollView2.scrollTo(x, y);
		} else if (scrollView == scrollView2) {
			scrollView1.scrollTo(x, y);
		}

	}
}